package com.qq.BFMRSE.frame;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import com.qq.BFMRSE.entity.user;
import com.qq.BFMRSE.requestModule.registerModule;
import com.qq.BFMRSE.util.BFConstants;

public class registerFrame extends JFrame implements ActionListener{

	private JLabel userNameLabel;
	private JLabel emailLabel;
	private JTextField userNameTF;
	private JTextField emailTF;
	private JButton regiterBtn;
	private JLabel pwdLabel;
	private JLabel rePwdlabel;
	private JPasswordField pwdField;
	private JPasswordField rePwdField;

	/**
	 * Create the frame.
	 */
	public registerFrame() {
		init();
		this.setTitle("新用户注册界面");
        ImageIcon imgBF=new ImageIcon("images\\BFMRSE.png");
        this.setIconImage(imgBF.getImage()); 
		getContentPane().setLayout(null);
		regiterBtn.addActionListener(this);
		setSize(500, 300);
		setVisible(true);
		setResizable(true);// 禁止拉边框拉长拉断
		setLocationRelativeTo(null);
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void init(){
		userNameLabel = new JLabel("用户名:");
		emailLabel= new JLabel("邮箱：");
		userNameTF = new JTextField(10);
		emailTF= new JTextField(10);
		regiterBtn = new JButton("注册");
		userNameLabel.setBounds(147, 56, 60, 30);
		getContentPane().add(userNameLabel);
		emailLabel.setBounds(147, 96, 60, 30);
		getContentPane().add(emailLabel);
		userNameTF.setBounds(253, 59, 100, 25);
		getContentPane().add(userNameTF);
		emailTF.setBounds(253, 99, 100, 25);
		getContentPane().add(emailTF);
		regiterBtn.setBounds(125, 221, 269, 30);
		getContentPane().add(regiterBtn);
		pwdLabel= new JLabel("密码：");
		pwdLabel.setBounds(147, 136, 60, 30);
		getContentPane().add(pwdLabel);
		rePwdlabel = new JLabel("确认密码：");
		rePwdlabel.setBounds(147, 179, 76, 30);
		getContentPane().add(rePwdlabel);
		pwdField = new JPasswordField();
		pwdField.setBounds(253, 139, 100, 25);
		getContentPane().add(pwdField);
		rePwdField = new JPasswordField();
		rePwdField.setBounds(253, 182, 100, 25);
		getContentPane().add(rePwdField);
		
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					registerFrame frame = new registerFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==regiterBtn){
			String userName=userNameTF.getText();
			String email=emailTF.getText();
			String pwd=pwdField.getText();
			String rePwd=rePwdField.getText();
			if("".equals(userName)||"".equals(email)||"".equals(pwd)||"".equals(rePwd)){
				JOptionPane.showMessageDialog(this,  "请填写完整信息！",
						"提示",JOptionPane.INFORMATION_MESSAGE);
			}else{
				Pattern emailPattern=Pattern.compile(BFConstants.CHECKEMAIL);
				Matcher emailMatcher=emailPattern.matcher(email);
				if(!emailMatcher.matches()){
					JOptionPane.showMessageDialog(this,  "邮箱格式不正确，请检查后重新填写！",
							"提示",JOptionPane.INFORMATION_MESSAGE);
				}
				if(!pwd.equals(rePwd)){
					JOptionPane.showMessageDialog(this,  "两次密码填写不同，请重新填写",
							"提示",JOptionPane.INFORMATION_MESSAGE);
					pwdField.setText("");
					rePwdField.setText("");
				}else {
					Pattern pwdPattern=Pattern.compile(BFConstants.CHECKPWD);
					Matcher pwdmacher=pwdPattern.matcher(pwd);
					if(!pwdmacher.matches()){
						JOptionPane.showMessageDialog(this,  "密码格式不正确，至少由6位字母和数字组成！",
								"提示",JOptionPane.INFORMATION_MESSAGE);
					}
					else{
						String registerUrl=BFConstants.URL+BFConstants.URLSPE+BFConstants.REGISTERNEWUSER;
						user newUser=new user(userName, email, rePwd);
						int flag=registerModule.registerNewUser(registerUrl, newUser);
						if(flag==1){
							JOptionPane.showMessageDialog(this,  "用户"+userName+",注册成功！",
									"提示",JOptionPane.INFORMATION_MESSAGE);
						}else if(flag==0){
							JOptionPane.showMessageDialog(this,   "用户名"+userName+"已经存在，请重新注册！",
									"提示",JOptionPane.INFORMATION_MESSAGE);
						}else if(flag==-1){
							JOptionPane.showMessageDialog(this, "注册失败，网络或者服务器出现问题！",
									"提示",JOptionPane.INFORMATION_MESSAGE);
						}
					}
					
				}
			}
		}
		
	}
}
